package cn.tedu.boot.ers.service.impl;

import cn.tedu.boot.ers.entity.Order;

import cn.tedu.boot.ers.entity.OrderProduct;
import cn.tedu.boot.ers.exception.ServiceException;
import cn.tedu.boot.ers.mapper.DishesMapper;
import cn.tedu.boot.ers.mapper.OrderMapper;
import cn.tedu.boot.ers.mapper.OrderProductMapper;
import cn.tedu.boot.ers.pojo.dto.OrderAddDTO;
import cn.tedu.boot.ers.pojo.vo.OrderDetailsVO;
import cn.tedu.boot.ers.pojo.vo.OrderProductDetailsVO;
import cn.tedu.boot.ers.service.IOrderService;
import cn.tedu.boot.ers.web.State;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {
    // // TODO
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderProductMapper orderProductMapper;
    @Autowired
    DishesMapper dishesMapper;

    @Override
    public void insert(OrderAddDTO orderAddDTO) {
        // 前提product表,dishes表,user表都已经有数据了
        // order里面的数值怎么来 ?  传一部分,查一部分(totalMoney)
        Order order = new Order();
        OrderProduct orderProduct = new OrderProduct();
        BeanUtils.copyProperties(orderAddDTO, order);
        // 得到totalMoney,并给totalMoney赋值
        BigDecimal totalMoney = dishesMapper.getTotalMoney(order.getUserId());
        order.setTotalMoney(totalMoney);
        // 由于创建订单时并不会有评论,所以将commentId设置为-1,表示此时评论不存在
        order.setCommentId(-1L);
        // orderMapper.insert(order)向数据库写入数据
        int rows = orderMapper.insert(order);
        log.info("即将先order表写入数据({})", order);
        if (rows != 1) {
            log.error("order写入数据异常");
            throw new ServiceException(State.ERR_INSERT, "服务器异常,请稍后再试");
        }
        // 给orderProduct赋值
        List<OrderProductDetailsVO> orderProductsList = dishesMapper.getOrderProductDetailsVO(order.getUserId());
        if (orderProductsList.isEmpty()) {
            throw new ServiceException(State.ERR_INSERT, "服务器异常,请稍后再试");
        }
        for (OrderProductDetailsVO item : orderProductsList) {
            orderProduct.setOrderId(order.getId());
            orderProduct.setProductId(item.getProductId());
            // 通过orderProductMapper.insert(OrderProduct)向order_product表写入数据
            log.info("即将先order_product表写入数据({})", orderProduct);
            int rowsI = orderProductMapper.insert(orderProduct);
            if (rowsI != 1) {
                log.error("order_product表写入数据异常");
                throw new ServiceException(State.ERR_INSERT, "服务器异常,请稍后再试");
            }
        }
        // order和order-product写入数据成功后,将dishes里面的东西删除掉
        int rowsD = dishesMapper.deleteByUserId(order.getUserId());
        if (rowsD == 0) {
            throw new ServiceException(State.ERR_INSERT, "服务器异常,请稍后再试");
        }
        log.info("dishes表中的用户id:({})菜已经清空", order.getUserId());
    }

    @Override
    public List<OrderDetailsVO> getOrderDetails(Long userId) {
        List<OrderDetailsVO> list = orderMapper.getDetailsByUserId(userId);
        if (list.isEmpty()) {
            throw new ServiceException(State.ERR_BAD_REQUEST, "你还没有订单,请返回首页点餐");
        }
        return list;
    }

    @Override
    public List<OrderDetailsVO> getAllOrderDetails() {
        List<OrderDetailsVO> list = orderMapper.getAllDetails();
        if (list.isEmpty()) {
            throw new ServiceException(State.ERR_BAD_REQUEST, "餐馆暂时没有订单,请加大营销");
        }
        return list;
    }
}
